package com.pinecone.service.impl;

import com.pinecone.common.utils.TxVideoUtils;
import com.pinecone.common.utils.UUIDUtils;
import com.pinecone.dao.SysCloudConfigDao;
import com.pinecone.dao.SysCloudConfigOrgDao;
import com.pinecone.dto.LiveRoomDTO;
import com.pinecone.dto.LiveRoomResponseDTO;
import com.pinecone.entity.SysCloudConfigEntity;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.pinecone.dao.LiveRoomDao;
import com.pinecone.entity.LiveRoomEntity;
import com.pinecone.service.LiveRoomService;



@Service("liveRoomService")
public class LiveRoomServiceImpl implements LiveRoomService {
	@Autowired
	private LiveRoomDao liveRoomDao;
	@Autowired
    private SysCloudConfigOrgDao sysCloudConfigOrgDao;
	@Override
	public LiveRoomEntity queryObject(String liveRoomId){
		return liveRoomDao.queryObject(liveRoomId);
	}
	
	@Override
	public List<LiveRoomEntity> queryList(Map<String, Object> map){
		return liveRoomDao.queryList(map);
	}

    @Override
    public int queryLiveRoomResponseTotal(Map<String, Object> map) {
        return liveRoomDao.queryLiveRoomResponseTotal(map);
    }

    @Override
    public List<LiveRoomResponseDTO> queryLiveRoomResponseList(Map<String, Object> map) {
        return liveRoomDao.queryLiveRoomResponseList(map);
    }

    @Override
	public int queryTotal(Map<String, Object> map){
		return liveRoomDao.queryTotal(map);
	}
	
	@Override
	public void save(LiveRoomEntity liveRoom){
		liveRoomDao.save(liveRoom);
	}

    /**
     * 更新直播间，需要重置推流地址时间
     * @param liveRoomEntity
     */
	@Override
	public void update(LiveRoomEntity liveRoomEntity){
        SysCloudConfigEntity sysCloudConfigEntitiy = sysCloudConfigOrgDao.queryConfigByOrgId(liveRoomEntity.getOrgId());
        Map<String, String> playUrls = new HashMap<>();
        playUrls = generatePlayUrl(liveRoomEntity.getLiveRoomId(),sysCloudConfigEntitiy.getTcBizid());
        String pushURl = generatePushUrl(liveRoomEntity.getEventStartTime(),liveRoomEntity.getLiveRoomId(),
                sysCloudConfigEntitiy.getTcBizid(),sysCloudConfigEntitiy.getTcPushSecureKey());

        liveRoomEntity.setPushUrl(pushURl)
                .setLiveFlv(playUrls.get("liveFlv"))
                .setLiveHls(playUrls.get("liveHls"))
                .setLiveRtmp(playUrls.get("liveRtmp"));
        //更新直播间信息
        liveRoomDao.update(liveRoomEntity);
	}
	
	@Override
	public void delete(String liveRoomId){
		liveRoomDao.delete(liveRoomId);
	}
	
	@Override
	public void deleteBatch(String[] liveRoomIds){
		liveRoomDao.deleteBatch(liveRoomIds);
	}

    @Override
    public String createLiveRoom(Integer orgId, Integer customerId, LiveRoomDTO liveRoomDTO) {
        LiveRoomEntity liveRoomEntity = new LiveRoomEntity();
        BeanUtils.copyProperties(liveRoomDTO,liveRoomEntity);
        //设置默认
        String liveRoomId = UUIDUtils.generateShortUuid();
        liveRoomEntity.setLiveRoomId(liveRoomId)
                .setLiveStatus(0)
                .setOrgId(orgId)
                .setUserId(customerId)
                .setChatRoomId(liveRoomId);
        //保存直播间
        liveRoomDao.save(liveRoomEntity);
        //设置推流和播放地址
        //获取该机构的配置文件
        SysCloudConfigEntity sysCloudConfigEntitiy = sysCloudConfigOrgDao.queryConfigByOrgId(orgId);
        Map<String, String> playUrls = new HashMap<>();
        playUrls = generatePlayUrl(liveRoomEntity.getLiveRoomId(),sysCloudConfigEntitiy.getTcBizid());
        String pushURl = generatePushUrl(liveRoomEntity.getEventStartTime(),liveRoomEntity.getLiveRoomId(),
                sysCloudConfigEntitiy.getTcBizid(),sysCloudConfigEntitiy.getTcPushSecureKey());

        liveRoomEntity.setPushUrl(pushURl)
                .setLiveFlv(playUrls.get("liveFlv"))
                .setLiveHls(playUrls.get("liveHls"))
                .setLiveRtmp(playUrls.get("liveRtmp"));
        //更新直播间信息
        liveRoomDao.update(liveRoomEntity);
        return liveRoomEntity.getLiveRoomId();
    }

    @Override
    public void increaseOnlineVisitedNum(String liveRoomId) {
        liveRoomDao.increaseOnlineVisitedNum(liveRoomId);
    }
    @Override
    public void increaseRepalyVisitedNum(String liveRoomId) {
        liveRoomDao.increaseReplayVisitedNum(liveRoomId);
    }

    /**
     * 生成播放地址
     * @param streamId
     * @return
     */
    public Map<String, String > generatePlayUrl(String streamId,String bizid){
        Map<String, String> playUrls = new HashMap<>();
        playUrls.put("liveRtmp","rtmp://"+bizid+".liveplay.myqcloud.com/live/"+bizid+"_"+streamId+"");
        playUrls.put("liveFlv","http://"+bizid+".liveplay.myqcloud.com/live/"+bizid+"_"+streamId+".flv");
        playUrls.put("liveHls","http://"+bizid+".liveplay.myqcloud.com/live/"+bizid+"_"+streamId+".m3u8");
        return playUrls;
    }
    /**
     * 生成推流地址
     * @param startTime
     * @param streamId
     * @return
     */
    public String generatePushUrl(Date startTime, String streamId, String bizid, String pushKey){
        //过期时间设置为活动开始时间+24小时
        //System.out.print(streamId);
        Long txTime = (startTime.getTime() + 24*60*60*1000)/1000;
        StringBuffer stringBuffer = new StringBuffer();
        String pushUrl = stringBuffer.append("rtmp://")
                .append(bizid)
                .append(".livepush.myqcloud.com/live/").append(bizid)
                .append("_")
                .append(streamId)
                .append("?")
                .append("bizid=")
                .append(bizid).append("&")
                .append(createPushURL(bizid+"_"+streamId,txTime, pushKey))
                .toString();
        return pushUrl;
    }
    /**
     * 创建推流地址
     * @param streamId
     * @param txTime
     * @return
     */
    public String createPushURL(String streamId, long txTime, String pushKey) {

        return TxVideoUtils.createPushURL(pushKey ,streamId,txTime);
    }


}
